﻿
Partial Class App_Main
    Inherits MyUI.PageBaseApp
    Protected SortGridLocations As UI.SortControl


    Protected Sub BntImport_Click(sender As Object, e As System.EventArgs) Handles BntImport.Click


        'disabilitato perché la classe twitterclient ora ha bisogno di un oauth per andare
        'Dim d = WebApi.Twitter.TwitterClient.GetTweets(Me.TextBox1.Text, "")

        'Dim t = From tw As WebApi.Twitter.Tweets In d Select tw

        'Me.GridView1.DataSource = d
        'Me.GridView1.DataBind()


    End Sub

    Protected Sub BtnFoll_Click(sender As Object, e As System.EventArgs) Handles BtnFoll.Click

        'TODO da sistemare
        Dim oauth = New WebApi.Oauth.Oauth
        oauth.oauthConsumerSecret = "kLgJlMmE0iWToUUK3xF15K8f9dwYjYjJRHg3o7Zw"
        oauth.oauthConsumerKey = "N89RIjsDiPGuqzHDMteSQ"
        Dim TwitterClient = New WebApi.Twitter.TwitterClient(oauth)
        Dim d = TwitterClient.GetFollowers(Me.TextBox1.Text, "", "-1")

        For ct = 0 To d.ids.Count / 100

            Dim users = TwitterClient.GetUsersLookup("", String.Join(",", d.ids.GetRange(ct * 100, 100)))

            'Me.GridView2.DataSource = users
            'Me.GridView2.DataBind()

            ' gli user su db
            For Each u In users
                Dim Acc = DB.TAB.Account.Where("Account_code", "=", u.id_str).ToObj

                If Acc Is Nothing Then
                    Acc = New mDB.Account
                    DB.InsertOnSubmit(Acc)
                End If

                Acc.Account_Code = u.id_str
                Acc.Fullname = u.Name
                Acc.Username = u.screen_name
                Acc.Avatar = u.profile_image_url
                Acc.Location = u.location
                'Acc.Follower_Count = u.followers_count
                Acc.Description = u.description
                Acc.Url = u.url
                Acc.Account_Add_Info.Post_Count = u.statuses_count

                'Acc.LastUpdated = Date.MinValue

                DB.SubmitChanges()


            Next
        Next

    End Sub

    Protected Sub BtnFollTweets_Click(sender As Object, e As System.EventArgs) Handles BtnFollTweets.Click

        'Dim Accounts = DB.TAB.Account.OrderBy("postupdate").ToList

        'For Each Acc In Accounts

        '    If Acc.Account_Update.Post_Update_Date < Now Then

        '        'TODO da sistemare
        '        Dim oauth = New WebApi.Oauth.Oauth
        '        oauth.oauthConsumerSecret = "kLgJlMmE0iWToUUK3xF15K8f9dwYjYjJRHg3o7Zw"
        '        oauth.oauthConsumerKey = "N89RIjsDiPGuqzHDMteSQ"
        '        Dim twitterClient = New WebApi.Twitter.TwitterClient(oauth)
        '        Dim Tweets = twitterClient.GetTweets("", Acc.Account_Code)

        '        UI.Page.WriteLog(Acc.Fullname & " - " & Tweets.Count)

        '        If Tweets.Count > 0 Then

        '            For Each t In Tweets
        '                Dim Post = DB.TAB.AccountPost.Where("Post_Code", "=", t.id_str).ToObj

        '                If Post Is Nothing Then
        '                    Post = New mDB.Account_Post
        '                    DB.InsertOnSubmit(Post)
        '                End If

        '                Post.Post_Code = t.id_str
        '                Post.Account_Id = Acc.Account_Code
        '                'Post.PostText = Encoding.ASCII.GetString(Encoding.UTF8.GetBytes(t.text))
        '                Post.Post_Text = Encoding.Default.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Default, Encoding.UTF8.GetBytes(t.text)))
        '                Post.Pubb_Date = t.created_at


        '                Try
        '                    DB.SubmitChanges()

        '                Catch ex As Exception

        '                    DB.DataUpdate.Clear()
        '                    DB.DataInsert.Clear()

        '                End Try

        '            Next

        '            Acc.Account_Update.Post_Update_Date = Now.AddDays(30)
        '            DB.SubmitChanges()
        '        Else

        '            Threading.Thread.Sleep(1000)

        '        End If

        '    End If


        'Next



    End Sub

    Protected Sub BtnHashTag_Click(sender As Object, e As System.EventArgs) Handles BtnHashTag.Click
        'Dim hashtag As New Dictionary(Of String, Dictionary(Of Integer, mDB.Account_Hashtag))(StringComparer.CurrentCultureIgnoreCase)
        'Dim PostHashtag As New Dictionary(Of String, mDB.Post_Hashtag)
        'Dim TagID As Integer = 0

        'DB.ExecuteCommand("truncate table account_hashtag")
        'DB.ExecuteCommand("truncate table hashtag")
        'DB.ExecuteCommand("truncate table post_hashtag")

        'Dim r As New Regex("(?:(?<=\s)|^)#(\w*[A-Za-z_]+\w*)")

        'Dim Accounts = DB.TabAccount.ToList

        'For Each Acc In Accounts

        '    Dim posts = DB.TabPost.Where("Account_ID", "=", Acc.Account_Code).ToList
        '    UI.Page.WriteLog(Acc.Fullname & " - " & posts.Count)

        '    For Each post In posts

        '        Dim matches = r.Matches(post.Post_Text)
        '        For Each match As Match In matches

        '            Dim tag = match.Value
        '            If Not hashtag.ContainsKey(tag) Then
        '                TagID += 1
        '                hashtag.Add(tag, New Dictionary(Of Integer, mDB.Account_Hashtag))
        '                hashtag(tag).Add(0, New mDB.Account_Hashtag With {.Hashtag_Id = TagID, .Account_Id = 0})
        '            End If

        '            If Not hashtag(tag).ContainsKey(Acc.Id) Then
        '                hashtag(tag).Add(Acc.Id, New mDB.Account_Hashtag With {.Hashtag_Id = hashtag(tag)(0).Hashtag_Id, .Account_Id = Acc.Id})
        '            End If

        '            hashtag(tag)(Acc.Id).Post_Count += 1
        '            hashtag(tag)(0).Follower_Post_Count += 1

        '            Dim phKey = String.Format("{0}_{1}", hashtag(tag)(0).Hashtag_Id, post.Id)
        '            If Not PostHashtag.ContainsKey(phKey) Then
        '                PostHashtag.Add(phKey, New mDB.Post_Hashtag With {.Hashtag_Id = hashtag(tag)(0).Hashtag_Id, .Post_Id = post.Id})
        '            Else
        '                UI.Page.WriteLog(String.Format("Post hastag duplicato, hashtag {0}, post {1}", tag, post.Post_Text))
        '            End If

        '        Next
        '    Next
        'Next

        ''------------------------------------
        '' post_tag
        'Dim ct = 1
        'DB.InitMultiRowsInsert()
        'For Each ph In PostHashtag
        '    If ct Mod 100000 = 0 Then
        '        UI.Page.WriteLog(String.Format("Post hastag {0}", ct, PostHashtag.Count))
        '        DB.SubmitChanges()
        '        DB.InitMultiRowsInsert()
        '    End If

        '    DB.InsertRowOnSubmit(ph.Value)
        '    ct += 1
        'Next
        'DB.SubmitChanges()

        ''------------------------------------
        '' account_tag
        'ct = 1
        'DB.InitMultiRowsInsert()
        'For Each tag In hashtag
        '    For Each acctag In tag.Value
        '        If ct Mod 100000 = 0 Then
        '            UI.Page.WriteLog(String.Format("Account hastag {0}/{1}", ct, hashtag.Count))
        '            DB.SubmitChanges()
        '            DB.InitMultiRowsInsert()
        '        End If

        '        DB.InsertRowOnSubmit(acctag.Value)
        '        ct += 1
        '    Next
        'Next
        'DB.SubmitChanges()

        ''------------------------------------
        '' tag
        'ct = 1
        'DB.InitMultiRowsInsert()
        'For Each tag In hashtag
        '    If ct Mod 100000 = 0 Then
        '        UI.Page.WriteLog(String.Format("hastag {0}/{1}", ct, hashtag.Count))
        '        DB.SubmitChanges()
        '        DB.InitMultiRowsInsert()
        '    End If

        '    DB.InsertRowOnSubmit(New mDB.Hashtag With {.Id = tag.Value(0).Hashtag_Id, .Hashtag = tag.Key})
        '    ct += 1
        'Next
        'DB.SubmitChanges()




    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        SortGridLocations = New UI.SortControl(Me.GridUsers.ID, Me.ViewState, "fullname", SortDirection.Ascending)

        If Not Me.IsPostBack Then
            Session("TagSel") = Nothing
            SetDataHasttag()
            SetDataHasttagSel()

            SetDataQuery()


        Else
        End If


    End Sub

    Private Sub SetDataQuery()
        Dim q = DB.GetQuery

        Dim query = q.From("query").
            Join("query_post", "query_id", "query.id").
            SetSelect(
                "concat(query,' - ', count(query_post.id)) as query", "query.id").
            GroupBy("query_post.query_id").
            ToDataTable

        UI.List.Bind(Me.DdlQuery, query, "query", "id", "", False)



    End Sub
    Private Sub SetDataHasttagSel()

        Dim TagSel As List(Of Integer) = Session("TagSel")
        'If TagSel Is Nothing Then
        '    Me.RptHasttagSel.DataSource = Nothing
        'Else
        '    If TagSel.Count > 0 Then
        '        Dim Tags = DB.TabHashtag.Where("ID", "IN", TagSel).ToList
        '        Me.RptHasttagSel.DataSource = Tags

        '    End If


        'End If

        'Me.RptHasttagSel.DataBind()


    End Sub


    Private Sub SetDataHasttag()
        Dim qUseTag = DB.GetQuery

        qUseTag.From("Hashtag").
            Join("Account_Hashtag", "Hashtag_ID", "ID").
            Where("Account_ID", "=", 0).
            OrderBy("Follower_Post_Count desc").
            Limit(150).SetSelect("ID", "hashtag", "Follower_Post_Count")


        Dim Tags = qUseTag.ToDataTable  ' q.From(qUseTag, "tag", "").OrderBy("hashtag").SetSelect("*").ToDataTable

        Dim TagCounts = (From r In Tags.Rows Select r("Follower_Post_Count"))

        Dim min = TagCounts.Min
        Dim max = TagCounts.Max
        Utils.DT.ComputeData.Compute(Tags, "dim", "({'Follower_Post_Count'} - {p0}) / {p1}", min, (max - min) / 10)



        Me.RptHasttag.DataSource = Tags
        Me.RptHasttag.DataBind()


    End Sub

    Protected Sub RptHasttag_ItemCommand(source As Object, e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RptHasttag.ItemCommand

        Dim TagSel As List(Of Integer) = Session("TagSel")

        If TagSel Is Nothing Then
            TagSel = New List(Of Integer)
        End If

        TagSel.Add(e.CommandArgument)
        Session("TagSel") = TagSel

        SetDataHasttagSel()

    End Sub

    Protected Sub RptHasttagSel_ItemCommand(source As Object, e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RptHasttagSel.ItemCommand

        Dim TagSel As List(Of Integer) = Session("TagSel")

        TagSel.Remove(e.CommandArgument)

        SetDataHasttagSel()

    End Sub

    Protected Sub BtnSearch_Click(sender As Object, e As System.EventArgs) Handles BtnSearch.Click

        SetDataUsersTag()

    End Sub
    Private Sub SetDataUsersTag()
        Dim q = DB.GetQuery()

        Dim TagMin = Utils.Conv.ToInt(Me.TxtTweetsMin.Text)

        q.From("account").
            Join("account_hashtag", "Account_ID", "ID").
            Join("hashtag", "ID", "Hashtag_ID").
            GroupBy("account.ID").
            OrderBy("tot_post desc").
            SetSelect(
                "account.*", "sum(account_hashtag.Post_Count)as tot_post")

        Dim TagSel As List(Of Integer) = Session("TagSel")

        For Each tagID In TagSel
            q.WhereOp("or", "and", "Hashtag_ID", "=", tagID, "account_hashtag.Post_Count", ">=", TagMin)
        Next

        Dim Acc = SortGridLocations.SortQuery(q).ToDataTable

        Me.GridUsers.PageSize = 20
        UI.Grid.Bind(Me.GridUsers, Acc)
        Me.LblUserFound.Text = String.Format("Trovati {0} utenti", Acc.Rows.Count)

        '---------------------------
        SetDataTagFilter()

    End Sub

    Private Sub SetDataTagFilter()
        Dim q = DB.GetQuery()
        Dim qUseTag = DB.GetQuery

        Dim TagSel As List(Of Integer) = Session("TagSel")

        qUseTag.From("Post_Hashtag").
            SetSelect("post_id")

        For Each tagID In TagSel
            qUseTag.WhereOp("or", "and", "Hashtag_ID", "=", tagID)
        Next

        q.
            From(qUseTag, "p", "").
            Join("post_hashtag", "post_id", "p.post_id").
            Join("hashtag", "id", "post_hashtag.hashtag_id").
            GroupBy("hashtag_id").
            OrderBy("tot_tag DESC").
            SetSelect("hashtag_id", "hashTag.hashTag", "count(hashtag_id) as tot_tag").
            Limit(100)


        For Each tagID In TagSel
            q.Having("Hashtag_ID", "<>", tagID)
        Next

        Dim Tags = q.ToDataTable

        Dim TagCounts = (From r In Tags.Rows Select r("tot_tag"))

        Dim min = TagCounts.Min
        Dim max = TagCounts.Max
        Utils.DT.ComputeData.Compute(Tags, "dim", "({'tot_tag'} - {p0}) / {p1}", min, (max - min) / 10)



        Me.RptHasttagFilter.DataSource = Tags
        Me.RptHasttagFilter.DataBind()

    End Sub
    Protected Sub RptHasttagFilter_ItemCommand(source As Object, e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RptHasttagFilter.ItemCommand

        Dim TagSel As List(Of Integer) = Session("TagSel")

        If TagSel Is Nothing Then
            TagSel = New List(Of Integer)
        End If

        TagSel.Add(e.CommandArgument)
        Session("TagSel") = TagSel

        SetDataHasttagSel()

    End Sub


    Private Sub sSetDataUsers()

        Dim acc = SortGridLocations.SortQuery(DB.TAB.Account).ToList

        Me.GridUsers.PageSize = 20
        UI.Grid.Bind(Me.GridUsers, acc)

    End Sub


    Protected Sub GridUsers_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridUsers.PageIndexChanging

        Me.GridUsers.PageIndex = e.NewPageIndex

        Me.SetDataUsersTag()

    End Sub
    Protected Sub GridUsers_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridUsers.Sorting

        Me.SortGridLocations.Sort(e.SortExpression)

        Me.SetDataUsersTag()


    End Sub


    Protected Sub BtnCreateQuery_Click(sender As Object, e As System.EventArgs) Handles BtnCreateQuery.Click
        'Dim ct = 1

        'Dim query = DB.TabQuery.Where("Query", "=", Me.TxtQuery.Text).ToObj
        'If query Is Nothing Then
        '    query = New mDB.Query With {.Query = Me.TxtQuery.Text}
        '    DB.InsertOnSubmit(query)
        'End If
        'DB.SubmitChanges()

        'Dim oauth = New WebApi.Oauth.Oauth
        'oauth.oauthConsumerSecret = "kLgJlMmE0iWToUUK3xF15K8f9dwYjYjJRHg3o7Zw"
        'oauth.oauthConsumerKey = "N89RIjsDiPGuqzHDMteSQ"
        'Dim TwitterClient = New WebApi.Twitter.TwitterClient(oauth)

        'For ct = 1 To 15

        '    Dim res = TwitterClient.GetSearch(query.Query, ct)
        '    Dim Tweets = res.results

        '    If Tweets.Count > 0 Then

        '        For Each t In Tweets
        '            Dim Post = DB.TabQuery_Post.Where("Post_Code", "=", t.id_str).ToObj

        '            If Post Is Nothing Then
        '                Post = New mDB.Query_Post
        '                DB.InsertOnSubmit(Post)
        '            End If

        '            Post.Post_Code = t.id_str
        '            Post.Post_Text = Encoding.Default.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Default, Encoding.UTF8.GetBytes(t.text)))
        '            Post.Query_Id = query.Id

        '            Try
        '                DB.SubmitChanges()

        '            Catch ex As Exception

        '                DB.DataUpdate.Clear()
        '                DB.DataInsert.Clear()

        '            End Try

        '        Next

        '        DB.SubmitChanges()

        '    End If

        '    UI.Page.WriteLog(String.Format("pagina {0} di 15 ({1})", ct, Tweets.Count))

        'Next

        ''--------------------------------
        'DB.TabQuery_Hashtag.Delete.Where("query_id", "=", query.Id).ExecuteCommand()

        'Dim r As New Regex("(?:(?<=\s)|^)#(\w*[A-Za-z_]+\w*)")

        'Dim posts = DB.TabQuery_Post.Where("query_id", "=", query.Id).ToList
        'Dim hashtag As New Dictionary(Of String, mDB.Query_Hashtag)(StringComparer.CurrentCultureIgnoreCase)
        ''UI.Page.WriteLog(Acc.Fullname & " - " & posts.Count)

        'For Each post In posts

        '    Dim matches = r.Matches(post.Post_Text)
        '    For Each match As Match In matches

        '        Dim tag = Encoding.UTF8.GetString(Encoding.GetEncoding("ISO-8859-8").GetBytes(match.Value)) '   Encoding.ASCII.GetString(Encoding.Convert(Encoding.UTF8, Encoding.ASCII, Encoding.UTF8.GetBytes(match.Value)))
        '        If Not hashtag.ContainsKey(tag) Then
        '            'TagID += 1
        '            hashtag.Add(tag, New mDB.Query_Hashtag With {.Hashtag = tag, .Post_Count = 0, .Query_Id = query.Id})
        '        End If
        '        hashtag(tag).Post_Count += 1

        '    Next
        'Next

        ''------------------------------------
        '' tag
        'hashtag.Remove(query.Query)
        'ct = 1
        'DB.InitMultiRowsInsert()
        'For Each tag In hashtag
        '    If ct Mod 100000 = 0 Then
        '        UI.Page.WriteLog(String.Format("hastag {0}/{1}", ct, hashtag.Count))
        '        DB.SubmitChanges()
        '        DB.InitMultiRowsInsert()
        '    End If

        '    DB.InsertRowOnSubmit(tag.Value)
        '    ct += 1
        'Next
        'DB.SubmitChanges()

    End Sub

    Protected Sub BntViewQuery_Click(sender As Object, e As System.EventArgs) Handles BntViewQuery.Click
        Dim q = DB.GetQuery()

        q.From("query_hashtag").
            Where("query_id", "=", Me.DdlQuery.SelectedValue).
            OrderBy("post_count desc").
            Limit(150).SetSelect("hashtag", "post_count")

        Dim Tags = q.ToDataTable

        Dim TagCounts = (From r In Tags.Rows Select r("post_count"))

        Dim min = TagCounts.Min
        Dim max = TagCounts.Max
        Utils.DT.ComputeData.Compute(Tags, "dim", "({'post_count'} - {p0}) / {p1}", min, (max - min) / 10)



        Me.RptHashtagQuery.DataSource = Tags
        Me.RptHashtagQuery.DataBind()

        Me.SetDataQuery()

    End Sub
End Class
